<?php
/**
 * 春哥团队（www.cgtblog.com） - 春哥多端多商户万能DIY商城系统
 * Copyright © 2024 春哥技术团队 保留所有权利
 * =========================================================

 * ----------------------------------------------
 * 您只能在商业授权范围内使用，不可二次转售、分发、分享、传播
 * 任何企业和个人不得对代码以任何目的任何形式的再发布
 * =========================================================
 */

// +----------------------------------------------------------------------
// | 后台账号 子账号
// +----------------------------------------------------------------------
namespace app\controller;
use think\facade\View;
use think\facade\Db;
use app\model\Member as m;
class Member extends Common
{
    public function initialize(){
		parent::initialize();
		if(bid > 0) showmsg('无访问权限');
	}
	//会员列表
    public function index(){
		$levelArr = Db::name('member_level')->where('aid',aid)->order('sort,id')->column('name','id');
		$defaultCat = Db::name('member_level_category')->where('aid',aid)->where('isdefault', 1)->value('id');
		if($defaultCat > 0) {
            $levelIdsDefault = Db::name('member_level')->where('aid',aid)->where('cid', $defaultCat)->column('id');
        }

		if(request()->isAjax()){
			$page = input('param.page');
			$limit = input('param.limit');
			if(input('param.field') && input('param.order')){
				$order = input('param.field').' '.input('param.order');
			}else{
				$order = 'id desc'; 
			}
			$where = [];
			$where[] = ['aid','=',aid];
			if(input('param.mid')) {
                if(getcustom('order_show_onlychildren')) {
                    if ($this->admin['order_show_onlychildren']) {
                        $uid = Db::name('admin_user')->where('aid',aid)->where('bid',bid)->order('id','asc')->value('id');
                        if($uid != $this->uid){
                            $childmids = \app\common\Member::getdownmids(aid,$this->user['mid']);
                            if(in_array(input('param.mid'),$childmids))
                                $where[] = ['id','=',input('param.mid')];
                            else
                                $where[] = ['id','in',$childmids];
                        }
                    }else{
                        $where[] = ['id','=',input('param.mid')];
                    }
                }else{
                    $where[] = ['id','=',input('param.mid')];
                }
            }else{
                if(getcustom('order_show_onlychildren')) {
                    if ($this->admin['order_show_onlychildren']) {
                        $uid = Db::name('admin_user')->where('aid',aid)->where('bid',bid)->order('id','asc')->value('id');
                        if($uid != $this->uid){
                            $childmids = \app\common\Member::getdownmids(aid,$this->user['mid']);
                            if(in_array(input('param.mid'),$childmids))
                                $where[] = ['id','=',input('param.mid')];
                            else
                                $where[] = ['id','in',$childmids];
                        }
                    }
                }else{
                    if(input('param.card_id')) {
                        $mids = Db::name('membercard_record')->where('aid',aid)->where('card_id',input('param.card_id'))->column('mid');
                        $where[] = ['id','in',$mids];
                    }
                }
            }

			if(input('param.pid')) $where[] = ['pid','=',input('param.pid')];
			if(input('param.isgetcard')){
				if(input('param.isgetcard') == 1){
					$where[] = ['','exp',Db::raw('card_code is not null')];
				}else{
					$where[] = ['','exp',Db::raw('card_code is null')];
				}
			}
			if(input('param.nickname')) $where[] = ['nickname|tel|realname|card_code','like','%'.input('param.nickname').'%'];
           
            if(input('param.realname')) $where[] = ['realname','like','%'.input('param.realname').'%'];
			if(input('param.levelid')) {
			    if($levelIdsDefault && !in_array(input('param.levelid'),$levelIdsDefault)) {
                    $mids = Db::name('member_level_record')->where('aid',aid)->where('levelid', input('param.levelid'))->column('mid');
                    $where[] = ['id','in',$mids];
                } else
                    $where[] = ['levelid','=',input('param.levelid')];
            }

			//其他分组等级的筛选

			if(input('param.ctime') ){
				$ctime = explode(' ~ ',input('param.ctime'));
				$where[] = ['createtime','>=',strtotime($ctime[0])];
				$where[] = ['createtime','<',strtotime($ctime[1]) + 86400];
			}

			if(input('param.fhid')){
				$midlist = Db::name('shop_order_fenhong')->where('aid',aid)->where('id',input('param.fhid'))->value('midlist');
				$where[] = ['id','in',$midlist];
			}
            if(getcustom('plug_tengrui')){
                if(input('?param.tr_is_rzh') && input('param.tr_is_rzh')!==''){
                    $where[] = ['tr_is_rzh','=',input('param.tr_is_rzh')];
                }
            }
            if(getcustom('xixie')){
                if(input('?param.mdid') && input('param.mdid')!==''){
                    $where[] = ['mdid','=',input('param.mdid')];
                }
            }

			if(input('param.fxmid')){
				$fxmid = input('param.fxmid/d');
				if(input('param.deep') == 1){
					$where[] = ['pid','=',$fxmid];
				}elseif(input('param.deep') == 2){
					$where[] = Db::raw("pid in(select id from ".table_name('member')." where pid=".$fxmid.")");
				}elseif(input('param.deep') == 3){
					$where[] = Db::raw("pid in(select id from ".table_name('member')." where pid in(select id from ".table_name('member')." where pid=".$fxmid."))");
				}
			}
            if(getcustom('product_glass')){
                $from = input('param.from');
                if($from=='glassrecord'){
                    //只取下过单的商城或预约 客户要求暂时屏蔽
                    /*$mids1 = Db::name('shop_order')->where('aid',aid)->where('bid',bid)->where('status','in',[1,2,3])->column('mid');
                    $mids2 = Db::name('yuyue_order')->where('aid',aid)->where('bid',bid)->where('status','in',[1,2,3])->column('mid');
                    $mids = ['-1'];
                    if($mids1){
                        $mids = array_unique($mids1);
                    }
                    if($mids2){
                        $mids = array_unique(array_merge($mids,$mids2));
                    }
                    $where[] = ['id','in',$mids];*/
                }
            }

			$count = 0 + Db::name('member')->where($where)->count();
			$data = Db::name('member')->where($where)->page($page,$limit)->order($order)->select()->toArray();
            if(getcustom('plug_yuebao')){
                $set  = Db::name('admin_set')->where('aid',aid)->field('yuebao_rate,yuebao_withdraw_time')->find();
            }
            
			//会员标签
			if(getcustom('member_tag')){
				$tagdata = Db::name('member_tag')->where('aid',aid)->column('name','id');
			}

            $moeny_weishu = 2;
            if(getcustom('fenhong_money_weishu')){
                $moeny_weishu = Db::name('admin_set')->where('aid',aid)->value('fenhong_money_weishu');
            }
            $score_weishu = 0;
            if(getcustom('score_weishu')){
                $score_weishu = Db::name('admin_set')->where('aid',aid)->value('score_weishu');
            }

            if(getcustom('fenhong_max') ){
                $fhlevellist = Db::name('member_level')->where('aid',aid)->where('fenhong','>','0')->order('sort desc,id desc')->column('*','id');
                $sysset = Db::name('admin_set')->where('aid',aid)->find();
                if(!empty($sysset['fenhong_max_add'])){
                    foreach($fhlevellist as $k=>$v){
                        $fenhong_max = Db::name('member_level')
                            ->where('aid',aid)
                            ->where('sort','<',$v['sort'])
                            ->sum('fenhong_max_money');
                        $fhlevellist[$k]['fenhong_max_money'] = bcadd($v['fenhong_max_money'],$fenhong_max,2);
                    }
                }
            }
			foreach($data as $k=>$v){

                if(getcustom('plug_yuebao')){
                    $data[$k]['self_yuebao_rate']     = $v['yuebao_rate'];
                    if($v['yuebao_rate']<0){
                        $data[$k]['yuebao_rate']      = $set['yuebao_rate'];
                    }

                    $data[$k]['self_yuebao_withdraw_time'] = $v['yuebao_withdraw_time'];
                    if($v['yuebao_withdraw_time']<0){
                        $data[$k]['yuebao_withdraw_time']  = $set['yuebao_withdraw_time'];
                    }
                }

				$data[$k]['levelname'] = $levelArr[$v['levelid']];
				if(getcustom('plug_sanyang')) {
                    $level_ids = Db::name('member_level_record')->where('aid', aid)->where('mid',$v['id'])->column('levelid');
                    if($level_ids) {
                        foreach ($level_ids as $lid)
                        $data[$k]['levelname'] .= '<br>'.$levelArr[$lid];
                    }
                }
				if(getcustom('member_level_down_commission')){
					$userlevel = Db::name('member_level')->field('id,name,down_level_totalcommission,down_level_id2')->where('aid', aid)->where('id',$v['levelid'])->find();
					$isauto_down = $v['isauto_down'];
					$showleveldown=true;
					if(!$v['isauto_down'] && $userlevel['down_level_totalcommission']>0){
						$leveldown = Db::name('member_level')->field('id,name')->where('id',$userlevel['down_level_id2'])->find();
						$leveldowncommission =$userlevel['down_level_totalcommission']-($v['totalcommission']-$v['down_commission']);
						$data[$k]['levelname'].='<br>还差'.$leveldowncommission.'佣金，降为'.$leveldown['name'];
					}else if($v['isauto_down']==1){
						$leveldown = Db::name('member_level')->field('id,name,recovery_level_proid')->where('id',$v['up_levelid'])->find();
						$proids = explode(',',$leveldown['recovery_level_proid']);
						$pro = Db::name('shop_product')->field('id,name')->where('id','in',$proids)->select()->toArray();
						$pros=[];
						foreach($pro as $p){
							$pros[] = $p['name'];
						}
						$data[$k]['levelname'].='<br>已降级，需购买'.implode(',',$pros).'才可恢复等级';
					}
				}

				if($v['pid']){
					$parent = Db::name('member')->where('aid',aid)->where('id',$v['pid'])->find();
				}else{
					$parent = array();
				}
                if($v['pid_origin']){
                    $parent_origin = Db::name('member')->where('aid',aid)->where('id',$v['pid_origin'])->find();
                }else{
                    $parent_origin = array();
                }

                if(getcustom('plug_tengrui')){
                    //查询对应的小区
                    $community_room = Db::name('member_community_room')
                        ->where('mid',$v['id'])
                        ->where('is_del',0)
                        ->field('tr_roomName,tr_relationType')
                        ->select()
                        ->toArray();
                    if($community_room){
                        $community_infor = '';
                        foreach($community_room as $cv){
                            if($data[$k]['community_infor']){
                                $community_infor .= "<br> ".$cv['tr_roomName'];
                            }else{
                                $community_infor .= $cv['tr_roomName'];
                            }
                            if($cv['tr_relationType'] == 0){
                                $community_infor .= " 业主";
                            }else if($cv['tr_relationType'] == 1){
                                $community_infor .= " 家属";
                            }else if($cv['tr_relationType'] == 2){
                                $community_infor .= " 租户";
                            }else if($cv['tr_relationType'] == 3){
                                $community_infor .= " 买断";
                            }else if($cv['tr_relationType'] == 4){
                                $community_infor .= " 租用";
                            }
                            $data[$k]['community_infor'] = $community_infor;
                        }
                        unset($cv);
                    }else{
                        $data[$k]['community_infor'] = '';
                    }
                }
                if(getcustom('xixie')) {
                    $data[$k]['mendian_infor'] = '';
                    if($v['mdid']){
                        $mendian = Db::name('mendian')->where('id',$v['mdid'])->where('aid',aid)->field('id,name')->find();
                        if($mendian){
                            $data[$k]['mendian_infor'] = 'ID:'.$mendian['id']."<br>"."名称:".$mendian['name'];
                        }
                    }
                }
				$data[$k]['parent'] = $parent;
                $data[$k]['parent_origin'] = $parent_origin;
                $data[$k]['money'] = \app\common\Member::getmoney($v);
                $data[$k]['score'] = \app\common\Member::getscore($v);
                $data[$k]['score'] = dd_money_format($v['score'],$score_weishu);
				if(getcustom('memberlist_showteamyeji')){
					$downmids = \app\common\Member::getdownmids(aid,$v['id']);
					if($downmids){
						$teamyeji = Db::name('shop_order_goods')->where('aid',aid)->where('mid','in',$downmids)->where('status','in','1,2,3')->sum('real_totalprice');
						$data[$k]['teamyeji'] = $teamyeji;
                        $data[$k]['ordercount'] = Db::name('shop_order')->where('aid',aid)->where('mid','in',$downmids)->where('status','in','1,2,3')->count();
						$data[$k]['downcount'] = count($downmids);
					}else{
						$data[$k]['teamyeji'] = 0;
                        $data[$k]['ordercount'] = 0;
						$data[$k]['downcount'] = 0;
					}
				}
				
				//会员标签
				if(getcustom('member_tag')){
					if($v['tags']) $tags = explode(',',$v['tags']);
					 foreach ($tags as $t){
						  $data[$k]['tagname'] .= '<br>'.$tagdata[$t];
					 }
				}
				$data[$k]['commission'] = dd_money_format($v['commission'],$moeny_weishu);
				//查询股东分红上限数据
                if(getcustom('fenhong_max')){
                    if($v['fenhong_max']>0){
                        $data[$k]['gudong_max'] = $v['fenhong_max'];
                    }else{
                        $data[$k]['gudong_max'] = $fhlevellist[$v['levelid']]['fenhong_max_money']?:0;
                    }
                    //查询会员已获得股东分红
                    $where_fenhong = [];
                    $where_fenhong[] = ['mid','=',$v['id']];
                    $where_fenhong[] = ['type','=','fenhong'];
                    $gudong_name = t('股东分红',aid);
                    $fenhong_total = Db::name('member_fenhonglog')
                        ->where($where_fenhong)
                        ->where('remark like "%'.$gudong_name.'%"')
                        ->sum('commission');
                    $data[$k]['gudong_total'] = bcmul($fenhong_total,1,2);
                }
			}
			return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data,'role'=>$role]);
		}

		if($this->auth_data == 'all' || in_array('Member/*',$this->auth_data) || in_array('Member/recharge',$this->auth_data)){
			$haverechargeAuth = true;
		}else{
			$haverechargeAuth = false;
		}
		if($this->auth_data == 'all' || in_array('Member/*',$this->auth_data) || in_array('Member/addscore',$this->auth_data)){
			$haveaddscoreAuth = true;
		}else{
			$haveaddscoreAuth = false;
		}
		if($this->auth_data == 'all' || in_array('Member/*',$this->auth_data) || in_array('Member/addcommission',$this->auth_data)){
			$haveaaddcommissionAuth = true;
		}else{
			$haveaaddcommissionAuth = false;
		}
        if(getcustom('pay_yuanbao') && ($this->auth_data == 'all' || in_array('Member/*',$this->auth_data) || in_array('Member/addyuanbao',$this->auth_data))){
            $haveaddyuanbaoAuth = true;
        }else{
            $haveaddyuanbaoAuth = false;
        }
        if(getcustom('member_friend') && ($this->auth_data == 'all' || in_array('Friend/*',$this->auth_data) || in_array('Friend/index',$this->auth_data))){
            View::assign('haveFriendAuth',true);
        }
        if(getcustom('member_overdraft_money') && ($this->auth_data == 'all' || in_array('OverdraftMoney/recharge',$this->auth_data))){
            View::assign('rechargeOverdraftMoneyAuth',true);
        }

		View::assign('aid',aid);
		View::assign('levelArr',$levelArr);
		$sort = true;
        if(getcustom('w7moneyscore')) {
            $w7moneyscore = db('admin_set')->where(['aid'=>aid])->value('w7moneyscore');
            if($w7moneyscore == 1)
            $sort = false;
        }

		$adminset = Db::name('admin_set')->where('aid',aid)->find();
        $admin = Db::name('admin')->where('id',aid)->find();

        View::assign('haverechargeAuth',$haverechargeAuth);
        View::assign('haveaddscoreAuth',$haveaddscoreAuth);
        View::assign('haveaaddcommissionAuth',$haveaaddcommissionAuth);
        View::assign('haveaddyuanbaoAuth',$haveaddyuanbaoAuth);
        View::assign('sort',$sort);
        View::assign('adminset',$adminset);
        View::assign('admin',$admin);
        if(getcustom('member_gongxian')){
            if($admin['member_gongxian_status']){
                if($this->auth_data == 'all' || in_array('Member/addgongxian',$this->auth_data)){
                    View::assign('haveaddgongxianAuth',true);
                }else{
                    View::assign('haveaddgongxianAuth',false);
                }
            }
        }
        if(getcustom('xixie')) {
            $mendian = Db::name('mendian')
                ->where('aid',aid)
                ->select()
                ->toArray();
            if(!$mendian){
                $mendian = [];
            }
            View::assign('mendian',$mendian);
        }
        if(getcustom('other_money')){
            //是否有多账户权限
            $othermoney_status = Db::name('admin')->where('id',aid)->value('othermoney_status');
            if($othermoney_status != 1){
                View::assign('othermoney_status',false);
            }else{
                View::assign('othermoney_status',true);
            }
            View::assign('haveaddmoney2Auth',true);
            View::assign('haveaddmoney3Auth',true);
            View::assign('haveaddmoney4Auth',true);
            View::assign('haveaddmoney5Auth',true);
            View::assign('haveaddfrozen_moneyAuth',true);
            if($this->auth_data != 'all' && !in_array('Member/*',$this->auth_data)){
                if(!in_array('Member/addmoney2',$this->auth_data)){
                    View::assign('haveaddmoney2Auth',false);
                }
                if(!in_array('Member/addmoney3',$this->auth_data)){
                    View::assign('haveaddmoney3Auth',false);
                }
                if(!in_array('Member/addmoney4',$this->auth_data)){
                    View::assign('haveaddmoney4Auth',false);
                }
                if(!in_array('Member/addmoney5',$this->auth_data)){
                    View::assign('haveaddmoney5Auth',false);
                }
                if(!in_array('Member/addfrozen_money',$this->auth_data)){
                    View::assign('haveaddfrozen_moneyAuth',false);
                }
            }
        }

		
		$business_selfscore = 0;
		if(getcustom('business_selfscore')){
			$bset = Db::name('business_sysset')->where('aid',aid)->find();
			if($bset['business_selfscore']==1 && $bset['business_selfscore2']==1){
				$business_selfscore = 1;
			}
		}

		View::assign('business_selfscore',$business_selfscore);
		return View::fetch();
    }
	//导出
	public function excel(){
		$levelList = Db::name('member_level')->where('aid',aid)->select()->toArray();
		$levelArr = array();
		foreach($levelList as $v){
			$levelArr[$v['id']] = $v['name'];
		}

		if(input('param.field') && input('param.order')){
			$order = input('param.field').' '.input('param.order');
		}else{
			$order = 'id desc';
		}
		$where = [];
		$where[] = ['aid','=',aid];
		if(input('param.mid')) $where[] = ['id','=',input('param.mid')];
		if(input('param.pid')) $where[] = ['pid','=',input('param.pid')];
		if(input('param.isgetcard')){
			if(input('param.isgetcard') == 1){
				$where[] = ['','exp',Db::raw('card_code is not null')];
			}else{
				$where[] = ['','exp',Db::raw('card_code is null')];
			}
		}
		if(input('param.nickname')) $where[] = ['nickname|tel|realname','like','%'.input('param.nickname').'%'];
		if(input('param.realname')) $where[] = ['realname','like','%'.input('param.realname').'%'];
		if(input('param.levelid')) $where[] = ['levelid','=',input('param.levelid')];
		if(input('param.ctime') ){
			$ctime = explode(' ~ ',input('param.ctime'));
			$where[] = ['createtime','>=',strtotime($ctime[0])];
			$where[] = ['createtime','<',strtotime($ctime[1]) + 86400];
		}

		if(input('param.fhid')){
			$midlist = Db::name('shop_order_fenhong')->where('aid',aid)->where('id',input('param.fhid'))->value('midlist');
			$where[] = ['id','in',$midlist];
		}

        if(getcustom('plug_tengrui')){
            if(input('?param.tr_is_rzh') && input('param.tr_is_rzh')!==''){
                $where[] = ['tr_is_rzh','=',input('param.tr_is_rzh')];
            }
        }
        if(getcustom('xixie')){
            if(input('?param.mdid') && input('param.mdid')!==''){
                $where[] = ['mdid','=',input('param.mdid')];
            }
        }
		

		if(input('param.fxmid')){
			$fxmid = input('param.fxmid/d');
			if(input('param.deep') == 1){
				$where[] = ['pid','=',$fxmid];
			}elseif(input('param.deep') == 2){
				$where[] = Db::raw("pid in(select id from ".table_name('member')." where pid=".$fxmid.")");
			}elseif(input('param.deep') == 3){
				$where[] = Db::raw("pid in(select id from ".table_name('member')." where pid in(select id from ".table_name('member')." where pid=".$fxmid."))");
			}
		}

		$list = Db::name('member')->where($where)->order($order)->select()->toArray();
        if(getcustom('pay_yuanbao')){
            $title = array('ID','头像','昵称','来源','推荐人','省份','城市','姓名','电话',t('余额'),t('积分'),t('佣金'),t('元宝'),'等级','加入时间','关注状态','公众号openid','小程序openid');
        }else{
            $title = array('ID','头像','昵称','来源','推荐人','省份','城市','姓名','电话',t('余额'),t('积分'),t('佣金'),'等级','加入时间','关注状态','公众号openid','小程序openid');
        }
        if(getcustom('plug_tengrui')){
            $title = array('ID','头像','昵称','来源','省份','城市','姓名','电话',t('余额'),t('积分'),t('佣金'),'等级','加入时间','关注状态','是否认证','小区','公众号openid','小程序openid');
        }
        if(getcustom('xixie')){
            $title = array('ID','头像','昵称','门店','来源','省份','城市','姓名','电话',t('余额'),t('积分'),t('佣金'),'等级','加入时间','关注状态','是否认证','小区','公众号openid','小程序openid');
        }
        $moeny_weishu = 2;
        if(getcustom('fenhong_money_weishu')){
            $moeny_weishu = Db::name('admin_set')->where('aid',aid)->value('fenhong_money_weishu');
        }
		$data = array();
		foreach($list as $k=>$vo){
			if($vo['platform']=='wx'){
				$vo['platform'] = '小程序';
			}elseif($vo['platform']=='mp'){
				$vo['platform'] = '公众号';
			}elseif($vo['platform']=='h5'){
				$vo['platform'] = 'H5';
			}
			if($vo['subscribe']==1){
				$vo['subscribe'] = '已关注';
			}else{
				$vo['subscribe'] = '未关注';
			}
            $vo['money'] = \app\common\Member::getmoney($vo);
            $vo['score'] = \app\common\Member::getscore($vo);
			$vo['levelname'] = $levelArr[$vo['levelid']];
			//上级
            $pmember = Db::name('member')->where('id',$vo['pid'])->field('id,nickname')->find();
            if($pmember){
                $vo['pmember'] = $pmember['nickname'].'(ID:'.$pmember['id'].')';
            }else{
                $vo['pmember'] = '暂无';
            }
            $vo['commission'] = dd_money_format($vo['commission'],$moeny_weishu);
            if(getcustom('xixie')) {
                $vo['mendian_infor'] = '无';
                if($vo['mdid']){
                    $mendian = Db::name('mendian')->where('id',$vo['mdid'])->where('aid',aid)->field('id,name')->find();
                    if($mendian){
                        $vo['mendian_infor'] = 'ID:'.$mendian['id']."\n\r"."名称:".$mendian['name'];
                    }
                }
                $data[] = array($vo['id'],$vo['headimg'],$vo['nickname'],$vo['mendian_infor'],$vo['platform'],$vo['province'],$vo['city'],$vo['realname'],$vo['tel'],$vo['money'],$vo['score'],$vo['commission'],$vo['levelname'],date('Y-m-d H:i',$vo['createtime']),$vo['subscribe'],$vo['mpopenid'],$vo['wxopenid']);
            }else if(getcustom('plug_tengrui')){
                if($vo['tr_is_rzh']==1){
                    $vo['tr_is_rzh'] = '已认证';
                }else{
                    $vo['tr_is_rzh'] = '未认证';
                }
                //查询对应的小区
                $community_room = Db::name('member_community_room')
                    ->where('mid',$vo['id'])
                    ->where('is_del',0)
                    ->field('tr_roomName,tr_relationType')
                    ->select()
                    ->toArray();
                if($community_room){
                    foreach($community_room as $cv){
                        if($community_infor){
                            $community_infor .= " \n\r ".$cv['tr_roomName'];
                        }else{
                            $community_infor = $cv['tr_roomName'];
                        }
                        if($cv['tr_relationType'] == 0){
                            $community_infor .= " 业主";
                        }else if($cv['tr_relationType'] == 1){
                            $community_infor .= " 家属";
                        }else if($cv['tr_relationType'] == 2){
                            $community_infor .= " 租户";
                        }else if($cv['tr_relationType'] == 3){
                            $community_infor .= " 买断";
                        }else if($cv['tr_relationType'] == 4){
                            $community_infor .= " 租用";
                        }
                    }
                    unset($cv);
                }else{
                    $community_infor = '';
                }
                $data[] = array($vo['id'],$vo['headimg'],$vo['nickname'],$vo['platform'],$vo['province'],$vo['city'],$vo['realname'],$vo['tel'],$vo['money'],$vo['score'],$vo['commission'],$vo['levelname'],date('Y-m-d H:i',$vo['createtime']),$vo['subscribe'],$vo['tr_is_rzh'],$community_infor,$vo['mpopenid'],$vo['wxopenid']);
            }else if(getcustom('pay_yuanbao')){
                $data[] = array($vo['id'],$vo['headimg'],$vo['nickname'],$vo['platform'],$vo['pmember'],$vo['province'],$vo['city'],$vo['realname'],$vo['tel'],$vo['money'],$vo['score'],$vo['commission'],$vo['yuanbao'],$vo['levelname'],date('Y-m-d H:i',$vo['createtime']),$vo['subscribe'],$vo['mpopenid'],$vo['wxopenid']);
            }else{
                
                $data[] = array($vo['id'],$vo['headimg'],$vo['nickname'],$vo['platform'],$vo['pmember'],$vo['province'],$vo['city'],$vo['realname'],$vo['tel'],$vo['money'],$vo['score'],$vo['commission'],$vo['levelname'],date('Y-m-d H:i',$vo['createtime']),$vo['subscribe'],$vo['mpopenid'],$vo['wxopenid']);
            }
		}
		$this->export_excel($title,$data);
	}
	//导入
	public function importexcel(){
		set_time_limit(0);
		ini_set('memory_limit',-1);
		$file = input('post.file');
		$exceldata = $this->import_excel($file);
		$levelList = Db::name('member_level')->where('aid',aid)->select()->toArray();
		$levelArr = array();
		foreach($levelList as $v){
			$levelArr[$v['name']] = $v['id'];
		}
		$defaultlevel = Db::name('member_level')->where('aid',aid)->where('isdefault',1)->find();

		$insertnum = 0;
		$updatenum = 0;
		foreach($exceldata as $data){
			if($data[6]){
				$indata = [];
				$indata['headimg'] = $data[0] ? $data[0] : PRE_URL.'/static/img/touxiang.png';
				$indata['nickname'] = preg_replace('/[\x{10000}-\x{10FFFF}]/u', ' ', $data[1]);
				$indata['platform'] = $this->getplatform(trim($data[2]));
				$indata['province'] = trim($data[3]);
				$indata['city'] = trim($data[4]);
				$indata['realname'] = trim($data[5]);
				$indata['tel'] = trim($data[6]);
				$indata['money'] = trim($data[7]);
				$indata['score'] = trim($data[8]);
				$indata['commission'] = trim($data[9]);
				$indata['levelid'] = $levelArr[$data[10]] ? $levelArr[$data[10]] : $defaultlevel['id'];
				$indata['subscribe'] = ($data[11] == '已关注' ? 1 : 0);
				$indata['mpopenid'] = trim($data[12]);
				$indata['wxopenid'] = trim($data[13]);
				if($data[14] && trim($data[14])!=''){
					$indata['pwd'] = md5(trim($data[14]));
				}
				$member = [];
				if(!$member && $indata['wxopenid']){
					$member = Db::name('member')->where('aid',aid)->where('wxopenid',$indata['wxopenid'])->find();
				}
				if(!$member && $indata['mpopenid']){
					$member = Db::name('member')->where('aid',aid)->where('mpopenid',$indata['mpopenid'])->find();
				}
				if(!$member && $indata['tel']){
					$member = Db::name('member')->where('aid',aid)->where('tel',$indata['tel'])->find();
				}
				if($indata['money'] === '') unset($indata['money']);
				if($indata['score'] === '') unset($indata['score']);
				if($indata['commission'] === '') unset($indata['commission']);
				if($indata['mpopenid'] == '') unset($indata['mpopenid']);
				if($indata['wxopenid'] == '') unset($indata['wxopenid']);
				if($indata['tel'] == '') unset($indata['tel']);
				if($member){
					if($indata['headimg'] == PRE_URL.'/static/img/touxiang.png') unset($indata['headimg']);
					if($indata['nickname'] == '') unset($indata['nickname']);
					if($indata['platform'] == '') unset($indata['platform']);
					if($indata['province'] == '') unset($indata['province']);
					if($indata['city'] == '') unset($indata['city']);
					if($indata['realname'] == '') unset($indata['realname']);
					if($indata['tel'] == '') unset($indata['tel']);
					if($indata['levelid'] == '') unset($indata['levelid']);
					if($indata['subscribe'] == '') unset($indata['subscribe']);
					if(getcustom('member_import')){
						if(trim($data[15]) && trim($data[16])){
							$pmember = Db::name('member')->where(['aid'=>aid,'tel'=>$data[15],'nickname'=>$data[16]])->find();
						}elseif(trim($data[15])){
							$pmember = Db::name('member')->where(['aid'=>aid,'tel'=>$data[15]])->find();
						}elseif(trim($data[16])){
							$pmember = Db::name('member')->where(['aid'=>aid,'tel'=>$data[16]])->find();
						}
						$indata['id'] = $member['id'];
						$mid = \app\model\Member::edit(aid,$indata);
					}else{
						Db::name('member')->where('aid',aid)->where('id',$member['id'])->update($indata);
					}
					$updatenum++;
				}else{
					if(getcustom('member_import')){
						if(trim($data[15]) && trim($data[16])){
							$pmember = Db::name('member')->where(['aid'=>aid,'tel'=>$data[15],'nickname'=>$data[16]])->find();
						}elseif(trim($data[15])){
							$pmember = Db::name('member')->where(['aid'=>aid,'tel'=>$data[15]])->find();
						}elseif(trim($data[16])){
							$pmember = Db::name('member')->where(['aid'=>aid,'nickname'=>$data[16]])->find();
						}
						$indata['aid'] = aid;
						$indata['sex'] = 3;
						$indata['createtime'] = time();
						$indata['pid'] = $pmember['id'];
						$mid = \app\model\Member::add(aid,$indata);
					}else{		
						$indata['aid'] = aid;
						$indata['sex'] = 3;
						$indata['createtime'] = time();
						Db::name('member')->insert($indata);
					}
					$insertnum++;
				}
			}
		}
		\app\common\System::plog('导入会员');
		return json(['status'=>1,'msg'=>'成功新增'.$insertnum.'条数据，修改'.$updatenum.'条数据']);
	}
	public function getplatform($str){
		if($str == '公众号'){
			return 'mp';
		}elseif($str == '小程序'){
			return 'wx';
		}elseif($str == 'H5'){
			return 'h5';
		}else{
			return $str;
		}
	}
	//会员充值
	public function recharge(){
		$mid = input('post.rechargemid/d');
		$money = floatval(input('post.rechargemoney'));
        $type = input('post.rechargetype');
		$actionname = '充值';
		if($money == 0 || $money == ''){
			return json(['status'=>0,'msg'=>'请输入金额']);
		}
		if($money < 0) $actionname = '扣费';
		if(session('IS_ADMIN')==0){
			$user = Db::name('admin_user')->where('aid',aid)->where('id',$this->uid)->find();
			$remark = '商家'.$actionname.'，操作员：'.$user['un'];
		}else{
			$remark = '商家'.$actionname;
		}
		$rs = \app\common\Member::addmoney(aid,$mid,$money,$remark,0,$type);
		\app\common\System::plog('给会员'.$mid.$actionname.'，金额'.$money);
		if($rs['status']==0) return json($rs);
        //充值余额
        if(getcustom('sms_temp_money_recharge')){
            $tel = Db::name('member')->where('aid',aid)->where('id',$mid)->value('tel');
            if($tel){
                $rs = \app\common\Sms::send(aid,$tel,'tmpl_money_recharge',['money'=>$money,'givemoney'=>0]);
            }
        }
		return json(['status'=>1,'msg'=>$actionname.'成功']);
	}
	//充值积分
	public function addscore(){
		$mid = input('post.id/d');
		$score = intval(input('post.score'));
		$remark = input('post.remark');
        $actionname = '增加';
		if($score == 0){
			return json(['status'=>0,'msg'=>'请输入'.t('积分').'数']);
		}
        if($score < 0) $actionname = '扣除';
		$rs = \app\common\Member::addscore(aid,$mid,$score,'后台修改：'.$remark);
		\app\common\System::plog('给会员'.$mid.$actionname.'积分'.$score);
		if($rs['status']==0) return json($rs);
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	//加佣金
	public function addcommission(){
		$mid = input('post.id/d');
		$commission = floatval(input('post.commission'));
		$remark = input('post.remark');
        $actionname = '增加';
		if($commission == 0){
			return json(['status'=>0,'msg'=>'请输入'.t('佣金').'金额']);
		}
        if($commission < 0) $actionname = '扣除';
		$rs = \app\common\Member::addcommission(aid,$mid,0,$commission,'后台修改：'.$remark,0, 'admin');
		\app\common\System::plog('给会员'.$mid.$actionname.t('佣金').$commission);
		if($rs['status']==0) return json($rs);
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	//编辑
	public function edit(){
		if(input('param.id')){
			$info = Db::name('member')->where('aid',aid)->where('id',input('param.id/d'))->find();
            $info['money'] = \app\common\Member::getmoney($info);
            $info['score'] = \app\common\Member::getscore($info);
			if(getcustom('member_tag')){
				$info['tags'] = explode(',',$info['tags']);
			}
		}else{
			$info = array('id'=>'');
		}
		$level_cid = Db::name('member_level_category')->where('aid',aid)->where('isdefault',1)->value('id');
        $level_cid = $level_cid ? $level_cid : 0;
		$levelList = Db::name('member_level')->where('aid',aid)->where('cid', $level_cid)->order('sort')->select()->toArray();
		$defaultlevel = Db::name('member_level')->where('aid',aid)->where('isdefault',1)->find();
		$levelArr = Db::name('member_level')->where('aid',aid)->order('sort')->column('name,areafenhong','id');
		if(getcustom('plug_sanyang')) {
            $level_category_extend = Db::name('member_level_category')->where('aid',aid)->where('isdefault',0)->select()->toArray();
            if($level_category_extend) {
                foreach ($level_category_extend as $key => $item) {
                    $level_category_extend[$key]['level_list'] = Db::name('member_level')->where('aid',aid)->where('cid', $item['id'])->order('sort')->select()->toArray();
                }
            }

            View::assign('level_category_extend',$level_category_extend);

            $levelExtendList = Db::name('member_level_record')->where('aid', aid)->where('mid', input('param.id'))->select()->toArray();
            View::assign('levelExtendList',$levelExtendList);
        }
		if(getcustom('register_fields')){
		    if($info['form_record_id']){
		        $record = Db::name('register_form_record')->where('aid',aid)->where('bid',bid)->where('id',$info['form_record_id'])->find();
                if(!empty($record)){
                    $formcontent = $record['content']?json_decode($record['content'],true):[];
                    $record['content'] = $formcontent;
                }else{
                    $record = [];
                }
            }
		    View::assign('register_record',$record);
        }

		//标签
		if(getcustom('member_tag')){
			$tagsdata =  Db::name('member_tag')->where('aid',aid)->where('status',1)->select()->toArray();
			View::assign('tagsdata',$tagsdata);
		}
        if(getcustom('member_area_agent_multi')){
            if($info['id']){
                $area_agent_info = [];
                $area_agent = Db::name('member_area_agent')->where('aid',aid)->where('mid',$info['id'])->select()->toArray();
                foreach ($area_agent as $item){
                    if($item['areafenhong_area']){
                        $area_agent_info[]=$item['areafenhong_province'].'-'.$item['areafenhong_city'].'-'.$item['areafenhong_area'];
                    }else{
                        if($item['areafenhong_city']){
                            $area_agent_info[]=$item['areafenhong_province'].'-'.$item['areafenhong_city'];
                        }else{
                            if($item['areafenhong_province']){
                                $area_agent_info[]=$item['areafenhong_province'];
                            }
                        }
                    }
                }
                View::assign('area_agent_info',jsonEncode($area_agent_info));
            }else{
                View::assign('area_agent_info',jsonEncode([]));
            }
        }


		View::assign('defaultlevel',$defaultlevel);
		View::assign('info',$info);
		View::assign('levelList',$levelList);
		View::assign('levelArr',$levelArr);
		$blist = Db::name('business')->field('id,name')->where('aid',aid)->order('sort desc,id')->select()->toArray();
        View::assign('blist',$blist);
        if(getcustom('xixie')) {
            $mendian = Db::name('mendian')
                ->where('aid',aid)
                ->select()
                ->toArray();
            if(!$mendian){
                $mendian = [];
            }
            View::assign('mendian',$mendian);
        }

        if(getcustom('member_set')){

            $set        = '';
            $setcontent = '';
            $log        = '';

            $set = Db::name('member_set')->where('aid',aid)->find();
            if($set){
                $setcontent = json_decode($set['content'],true);
                $log = Db::name('member_set_log')->where('formid',$set['id'])->where('mid',$info['id'])->where('aid',aid)->find();
            }

            View::assign('set',$set);
            View::assign('setcontent',$setcontent);
            View::assign('log',$log);
        }
		if(getcustom('member_levelup_auth')){
			if($this->auth_data == 'all' || in_array('Member/*',$this->auth_data) || in_array('Member/recharge',$this->auth_data)){
				$haverechargeAuth = true;
			}else{
				$haverechargeAuth = false;
			}
			View::assign('haverechargeAuth',$haverechargeAuth);
		}
		return View::fetch();
	}
	//保存
	public function save(){
		$info = input('post.info/a');
		$defaultlevel = Db::name('member_level')->where('aid',aid)->where('isdefault',1)->find();
		if($info['levelid'] == $defaultlevel['id'] || input('post.levelcq') == 1){
			$info['levelendtime'] = 0;
		}else{
			$info['levelendtime'] = strtotime($info['levelendtime']);
		}

        if($info['pwd']){
			$info['pwd'] = md5($info['pwd']);
		}else{
			unset($info['pwd']);
		}
       
		if(getcustom('member_tag')){
			$info['tags'] = implode(',',$info['tags']);
		}
		if(getcustom('member_richinfo')){
			$info['richinfo'] = \app\common\Common::geteditorcontent($info['richinfo']);
		}

		if($info['id']){
            $member = Db::name('member')->where('id',$info['id'])->find();
            if($info['paypwd']){
                $info['paypwd_rand'] = $member['paypwd_rand']?:make_rand_code(2,4);
                $info['paypwd'] = md5($info['paypwd'].$info['paypwd_rand']);
            }else{
                unset($info['paypwd']);
            }
			$rs = m::edit(aid,$info);
			if($rs['status'] == 0) return json($rs);
			\app\common\System::plog('编辑会员'.$info['id']);

            if(getcustom('member_vip_edit')){
                //1.原用户的等级 是普通用户 2. 修改的登记 不等于 当前用户的登记
                if($member['levelid'] == $defaultlevel['id'] && $info['levelid'] != $member['levelid']){
                    //增加升级记录
                    $order = [
                        'aid' => aid,
                        'mid' => $info['id'],
                        'from_mid' => 0,
                        'pid'=>$info['pid'],
                        'levelid' => $info['levelid'] ,
                        'title' => '后台升级',
                        'totalprice' => 0,
                        'createtime' => time(),
                        'levelup_time' => time(),
                        'beforelevelid' => $member['levelid'],
                        'form0' => '类型^_^后台升级',
                        'platform' => platform,
                        'status' => 2
                    ];
                    Db::name('member_levelup_order')->insert($order);
                }
            }
            //升级
            if($info['levelid'] != $member['levelid'] && $member['pid'] > 0){
                \app\common\Member::uplv(aid,$member['pid']);
            }
		}else{
            if($info['paypwd']){
                $info['paypwd_rand'] = make_rand_code(2,4);
                $info['paypwd'] = md5($info['paypwd'].$info['paypwd_rand']);
            }else{
                unset($info['paypwd']);
            }

			$info['aid'] = aid;
			$info['createtime'] = time();
			$info['platform'] = 'admin';
            $info['id'] = m::add(aid,$info);
			\app\common\System::plog('添加会员'.$info['id']);
            if(getcustom('member_vip_edit')){
                if($info['levelid'] != $defaultlevel['id']){
                    //增加升级记录
                    $order = [
                        'aid' => aid,
                        'mid' => $info['id'],
                        'from_mid' => 0,
                        'pid'=>$info['pid'],
                        'levelid' => $info['levelid'] ,
                        'title' => '后台升级',
                        'totalprice' => 0,
                        'createtime' => time(),
                        'levelup_time' => time(),
                        'beforelevelid' => $defaultlevel['id'],
                        'form0' => '类型^_^后台升级',
                        'platform' => platform,
                        'status' => 2
                    ];
                    Db::name('member_levelup_order')->insert($order);
                }
            }
            //升级
            if($info['pid'])
                \app\common\Member::uplv(aid,$info['pid']);

            if(getcustom('product_bonus_pool')){
                if($info['pid'] ){
                    $shop_set = Db::name('shop_sysset')->where('aid',aid)->find();
                    //判断人数
                    $parent = Db::name('member')->where('id',$info['pid'])->find();
                    $member_tj = $parent['bonus_pool_tjnum'] + 1;
                    //如果设置人数和父级的推荐人数相等  就奖励金 ,如果不相等 推荐人数+1
                    if($shop_set['bonus_pool_tuijian_num'] > 0 && $shop_set['bonus_pool_tuijian_num'] == $member_tj){
                        $pool = Db::name('bonus_pool')->where('aid',aid)->where('status',0)->order('id asc')->find();
                        $bonus_pool_money = dd_money_format($parent['bonus_pool_money'] + $pool['money']);
                        //增加log
                        $log = [
                            'aid' =>aid,
                            'mid' =>$parent['id'],
                            'frommid' => 0,
                            'commission' => $pool['money'],
                            'after' => $bonus_pool_money,
                            'createtime' => time(),
                            'remark' => '推荐用户奖金发放'
                        ];
                        Db::name('member_bonus_pool_log') ->insert($log);
                        //修改用户的钱 和 推荐数量清空
                        Db::name('member')->where('id',$info['pid'])->update(['bonus_pool_money' => $bonus_pool_money,'bonus_pool_tjnum' =>0]);
                        //修改奖金池状态
                        Db::name('bonus_pool')->where('aid',aid)->where('id',$pool['id'])->update(['status' => 1,'mid' => $parent['id']]);
                    }else{
                        Db::name('member')->where('id',$info['pid'])->update(['bonus_pool_tjnum' => $member_tj]);
                    }
                }
            }
		}

        if(getcustom('member_area_agent_multi')){
            //多代理区域
            $areainfo = input('post.areadata/a');
            Db::name('member_area_agent')->where('aid',aid)->where('mid',$info['id'])->delete();
            if($areainfo){
                foreach ($areainfo as $item){
                    $area = explode('-',$item);
                    $insert = [
                        'aid'=>aid,
                        'mid'=>$info['id'],
                        'createtime'=>time(),
                        'areafenhong_province'=>trim($area[0]),
                        'areafenhong_city'=>trim($area[1]),
                        'areafenhong_area'=>trim($area[2]),
                        'areafenhong'=>count($area),
                        'areafenhongbl'=>$info['areafenhongbl']
                    ];
                    Db::name('member_area_agent')->insert($insert);
                }
            }
        }

        if(getcustom('plug_sanyang')) {
            $record = input('post.record/a');
            if($record['level_extend_cq'] == 1){
                $record['levelendtime'] = 0;
            }else{
                $record['levelendtime'] = strtotime($record['levelendtime']);
            }
            unset($record['level_extend_cq']);

            if($record['levelid']) {
                $record['cid'] = Db::name('member_level')->where('aid',aid)->where('id',$record['levelid'])->value('cid');
            }
            if($record['id']) {
                if(empty($record['levelid'])) {
                    Db::name('member_level_record')->where('aid',aid)->where('mid', $info['id'])->where('id', $record['id'])->delete();
                } else {
                    $count = Db::name('member_level_record')->where('aid',aid)->where('mid', $info['id'])->where('cid', $record['cid'])->where('id','<>',$record['id'])->count();
                    if($count) {
                        return json(['status'=>0,'msg'=>'不能添加两个相同分组的等级']);
                    }
                    Db::name('member_level_record')->where('aid',aid)->where('mid', $info['id'])->where('id', $record['id'])->update($record);
                }
            } else {
                if($record['levelid']) {
                    $count = Db::name('member_level_record')->where('aid',aid)->where('mid', $info['id'])->where('cid', $record['cid'])->count();
                    if($count) {
                        return json(['status'=>0,'msg'=>'不能添加两个相同分组的等级']);
                    }
                    $record['aid'] = aid;
                    $record['mid'] = $info['id'];
                    $record['bid'] = bid;
                    $record['createtime'] = time();
                    Db::name('member_level_record')->insertGetId($record);
                }
            }
        }
        if(getcustom('register_fields')){
            $formdata = input('post.formdata/a');
            if($formdata && $formdata['id']){
                foreach ($formdata as $k=>$v){
                    if(is_array($v)){
                        $formdata[$k] = implode(',',$v);
                    }
                }
                Db::name('register_form_record')->where('aid',aid)->where('bid',bid)->where('id',$formdata['id'])->update($formdata);
            }
        }
        if(getcustom('member_set')){
            if(input('set')){
                $setinfo  = input('post.set/a');

                $set = Db::name('member_set')->where('aid',aid)->find();
                if(!$set){
                    return json(['status'=>0,'msg'=>'资料自定义设置不存在']);
                } 
                $setcontent = json_decode($set['content'],true);
                if(!$setcontent){
                    return json(['status'=>0,'msg'=>'资料自定义设置不存在']);
                }

                $data =[];
                foreach($setcontent as $k=>$v){
                    $value = $setinfo['form'.$k];
                    if(is_array($value)){
                        $value = implode(',',$value);
                    }
                    $data['form'.$k] = strval($value);
                    if($v['val3']==1 && $data['form'.$k]===''){
                        return json(['status'=>0,'msg'=>$v['val1'].' 必填']);
                    }
                }

                //查询是否设置过
                $log = Db::name('member_set_log')->where('mid',$info['id'])->where('formid',$set['id'])->where('aid',aid)->field('id')->find();
                if($log){
                    Db::name('member_set_log')->where('id',$log['id'])->update($data);
                }else{
                    $data['aid']    = aid;
                    $data['mid']    = $info['id'];
                    $data['formid'] = $set['id'];
                    $data['createtime'] = time();
                    $orderid = Db::name('member_set_log')->insertGetId($data);
                }
            }
        }
		return json(['status'=>1,'msg'=>'操作成功','url'=>(string)url('index')]);
	}
	//会员删除
	public function del(){
		$ids = input('post.ids/a');
		m::del(aid,$ids);
		\app\common\System::plog('删除会员'.implode(',',$ids));
		return json(['status'=>1,'msg'=>'删除成功']);
	}
    //编辑
	public function check(){
        $info = Db::name('member')->where('aid',aid)->where('id',input('param.id/d'))->find();
        if(getcustom('register_fields')){
            if($info['form_record_id']){
                $record = Db::name('register_form_record')->where('aid',aid)->where('bid',bid)->where('id',$info['form_record_id'])->find();
                if(!empty($record)){
                    $formcontent = $record['content']?json_decode($record['content'],true):[];
                    $record['content'] = $formcontent;
                }else{
                    $record = [];
                }
            }
            View::assign('register_record',$record);
        }
        if(getcustom('member_set')){

            $set        = '';
            $setcontent = '';
            $log        = '';

            $set = Db::name('member_set')->where('aid',aid)->find();
            if($set){
                $setcontent = json_decode($set['content'],true);
                $log = Db::name('member_set_log')->where('formid',$set['id'])->where('mid',$info['id'])->where('aid',aid)->find();
            }

            View::assign('set',$set);
            View::assign('setcontent',$setcontent);
            View::assign('log',$log);
        }
        View::assign('info',$info);
		return View::fetch();
	}
	//会员审核
	public function setst(){
		$id = input('post.id/d');
		$st = input('post.st/d');
		$checkreason = input('post.checkreason');
		$member = Db::name('member')->where('id',$id)->where('aid',aid)->find();
		if(!$member) return json(['status'=>0,'msg'=>'未找到该用户']);
		Db::name('member')->where('id',$id)->where('aid',aid)->update(['checkst'=>$st,'checkreason'=>$checkreason]);
		if($st == 1){
			$rs = \app\common\Sms::send(aid,$member['tel'],'tmpl_checksuccess',[]);
		}elseif($st == 2){
			$rs = \app\common\Sms::send(aid,$member['tel'],'tmpl_checkerror',['reason'=>$checkreason]);
		}
		
		//审核结果通知
		$tmplcontent = [];
		$tmplcontent['first'] = ($st == 1 ? '恭喜您的注册审核通过' : '抱歉您的注册未审核通过');
		$tmplcontent['remark'] = ($st == 1 ? '' : ($checkreason.'，')) .'请点击查看详情~';
		$tmplcontent['keyword1'] = '用户注册';
		$tmplcontent['keyword2'] = ($st == 1 ? '已通过' : '未通过');
		$tmplcontent['keyword3'] = date('Y年m月d日 H:i');
        $tempconNew = [];
        $tempconNew['thing9'] = '用户注册';
        $tempconNew['thing2'] = ($st == 1 ? '已通过' : '未通过');
        $tempconNew['time3'] = date('Y年m月d日 H:i');
		\app\common\Wechat::sendtmpl(aid,$id,'tmpl_shenhe',$tmplcontent,m_url('pages/my/usercenter'),$tempconNew);
		//订阅消息
		$tmplcontent = [];
		$tmplcontent['thing8'] = '用户注册';
		$tmplcontent['phrase2'] = ($st == 1 ? '已通过' : '未通过');
		$tmplcontent['thing4'] = $checkreason;
		
		$tmplcontentnew = [];
		$tmplcontentnew['thing2'] = '用户注册';
		$tmplcontentnew['phrase1'] = ($st == 1 ? '已通过' : '未通过');
		$tmplcontentnew['thing5'] = $checkreason;
		\app\common\Wechat::sendwxtmpl(aid,$id,'tmpl_shenhe',$tmplcontentnew,'pages/my/usercenter',$tmplcontent);

		return json(['status'=>1,'msg'=>'操作成功','res'=>$rs]);
	}
	//冻结 解冻
	public function setfreeze(){
		$id = input('post.id/d');
		$st = input('post.st/d');
		$member = Db::name('member')->where('id',$id)->where('aid',aid)->find();
		if(!$member) return json(['status'=>0,'msg'=>'未找到该用户']);
		Db::name('member')->where('id',$id)->where('aid',aid)->update(['isfreeze'=>$st]);
		return json(['status'=>1,'msg'=>'操作成功']);
	}
	//会员卡详情
	public function getcarddetail(){
		$card_id = input('post.card_id');
		$card_code = input('post.card_code');
		$info = Db::name('membercard_record')->where('aid',aid)->where('card_id',$card_id)->where('card_code',$card_code)->find();
		return json(['status'=>1,'data'=>$info]);
	}
	//更新用户的unionid 仅支持公众号 不支持小程序
	public function updateunionid(){
		$platform = input('param.platform');
		if(!$platform) $platform = 'mp';
		$url = 'https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token='.\app\common\Wechat::access_token(aid,$platform);
		$memberlist = Db::name('member')->field('id,mpopenid,unionid,headimg,nickname')->where('aid',aid)->where("mpopenid!='' and mpopenid is not null")->select()->toArray();
		$user_list = [];
		$thismemberlist = [];
		foreach($memberlist as $k=>$member){
			$user_list[] = ['openid'=>$member['mpopenid'],'lang'=>'zh_CN'];
			$thismemberlist[$member['mpopenid']] = $member;
			if(count($user_list) >= 100 || $k+1 == count($memberlist)){
				$rs = curl_post($url,jsonEncode(['user_list'=>$user_list]));
				$rs = json_decode($rs,true);
				if($rs['errcode']!=0 && $rs['errcode']!=1){
					return json(['status'=>0,'msg'=>\app\common\Wechat::geterror($rs)]);
				}
				$user_info_list = $rs['user_info_list'];
				foreach($user_info_list as $k2=>$v2){
					$thismember = $thismemberlist[$v2['openid']];
					if($v2['unionid'] && $v2['unionid'] != $thismember['unionid']){
						Db::name('member')->where('aid',aid)->where('id',$thismember['id'])->update(['unionid'=>$v2['unionid']]);
						echo "ID:{$thismember['id']}，昵称:{$thismember['nickname']} unionid已更新:{$v2['unionid']}<br>";
					}
				}
				$user_list = [];
				$thismemberlist = [];
			}
		}
		die('success');
	}

	private $deeplevel = 0;
	//关系图
	public function charts(){
		$mid = input('param.mid/d');
		$member = Db::name('member')->field('id,nickname,headimg,pid,realname,tel,levelid')->where('aid',aid)->where('id',$mid)->find();
		$leveldata = Db::name('member_level')->where('aid',aid)->order('sort,id')->column('id,sort,name','id');
		if($member){
		    $member['headimg'] =$member['headimg']?$member['headimg']: PRE_URL.'/static/img/touxiang.png';
			$member['children'] = $this->getdown($mid,0,$leveldata);
			$member['downcount'] = count($member['children']);
			$member['downcountall'] = Db::name('member')->where('aid',aid)->where('find_in_set('.$mid.',path)')->count();
			$member['name'] = $member['nickname'] . "\r\n" .$leveldata[$member['levelid']]['name'].'(ID:'.$member['id'].' 下级:'.$member['downcount'].'/'.$member['downcountall'].'人)';
			View::assign('member',$member);
			View::assign('deeplevel',$this->deeplevel);
		}
		return View::fetch();
	}

	//充值赠送
    public function registerGive()
    {
        if(request()->isPost()){
            $info = input('post.info/a');
            if($info['id']){
                Db::name('register_giveset')->where('aid',aid)->where('id',$info['id'])->update($info);
                \app\common\System::plog('编辑注册赠送'.$info['id']);
            }else{
                $info['aid'] = aid;
                $info['createtime'] = time();
                $id = Db::name('register_giveset')->insertGetId($info);
                \app\common\System::plog('编辑注册赠送'.$id);
            }
            return json(['status'=>1,'msg'=>'操作成功','url'=>(string)url('registerGive')]);
        }

        $info = Db::name('register_giveset')->where('aid',aid)->find();
        if($info) {
            $coupon_ids = explode(',', $info['coupon_ids']);
            $couponList = Db::name('coupon')->where('aid',aid)->where('bid', 0)->whereIn('id', $coupon_ids)->select()->toArray();
            $info['score'] = dd_money_format($info['score'],$this->score_weishu);
            $info['wanshan_score'] = dd_money_format($info['wanshan_score'],$this->score_weishu);
        }
        View::assign('couponList',$couponList);
        View::assign('info',$info);

        return View::fetch();
    }

	private function getdown($mid,$deeplevel,$leveldata){
		if($deeplevel > 20) return json([]);
		$deeplevel++;
		$memberlist = Db::name('member')->field('id,nickname,headimg,pid,pid_origin,realname,tel,levelid')->where('aid',aid)->where('pid',$mid)->select()->toArray();
		foreach($memberlist as $k=>$member){
			$memberlist[$k]['children'] = $this->getdown($member['id'],$deeplevel,$leveldata);
			$memberlist[$k]['downcount'] = count($memberlist[$k]['children']);
			$memberlist[$k]['downcountall'] = Db::name('member')->where('aid',aid)->where('find_in_set('.$member['id'].',path)')->count();
            if($member['pid_origin'])
                $memberlist[$k]['name'] = $member['nickname'] . "\r\n" . $leveldata[$member['levelid']]['name'].'(ID:'.$member['id'].' 下级:'.$memberlist[$k]['downcount'].'/'.$memberlist[$k]['downcountall'].'人'." 原推荐人ID:".$member['pid_origin'].')';
            else
			    $memberlist[$k]['name'] = $member['nickname'] . "\r\n" . $leveldata[$member['levelid']]['name'].'(ID:'.$member['id'].' 下级:'.$memberlist[$k]['downcount'].'/'.$memberlist[$k]['downcountall'].'人)';
		}
		if($this->deeplevel < $deeplevel) $this->deeplevel = $deeplevel;
		return $memberlist;
	}

	//会员path
	public function updatepath(){
		$memberlist = Db::name('member')->where('aid',aid)->order('id')->select()->toArray();
		foreach($memberlist as $member){
			$pathArr = $this->getpath($member,$path=[]);
			if($pathArr){
				$pathArr = array_reverse($pathArr);
				$pathstr = implode(',',$pathArr);
				Db::name('member')->where('aid',aid)->where('id',$member['id'])->update(['path'=>$pathstr]);
				//var_dump($member['id'].'----'.$pathstr);
				if($member['path'] != $pathstr){
					var_dump($member['path'].'----'.$pathstr);
				}
			}else{
				Db::name('member')->where('aid',aid)->where('id',$member['id'])->update(['path'=>'']);
			}
		}
		die('success');
	}
	public function getpath($member,$path,$deep=1){
		if($member['pid'] && $deep < 10){
			if(in_array($member['pid'],$path)) return $path;
			$path[] = $member['pid'];
			$parent = Db::name('member')->where('aid',aid)->where('id',$member['pid'])->find();
			$deep = $deep + 1;
			return $this->getpath($parent,$path,$deep);
		}
		return $path;
	}

	//增加开团次数
	public function addktnum(){
		$mid = input('post.ktmid/d');
		$ktnum = floatval(input('post.ktnum'));
		if($ktnum == 0){
			return json(['status'=>0,'msg'=>'请输入开团次数']);
		}
		if(session('IS_ADMIN')==0){
			$user = Db::name('admin_user')->where('aid',aid)->where('id',$this->uid)->find();
			$remark = '商家充值，操作员：'.$user['un'];
		}else{
			$remark = '商家充值';
		}
		$member = Db::name('member')->where('aid',aid)->where('id',$mid)->find();
        if(!$member) return ['status'=>0,'msg'=>t('会员').'不存在'];
		$after = $member['ktnum'] + $ktnum;
        Db::name('member')->where('aid',aid)->where('id',$mid)->update(['ktnum'=>$after]);
		\app\common\System::plog('给会员'.$mid.'增加开团次数'.$ktnum);
		return json(['status'=>1,'msg'=>'操作成功']);
	}

    //设置余额宝提现天数
    public function setywtime(){
        $mid = input('post.ywtmid/d');

        $ywrate = input('post.ywrate');
        $ywtime = input('post.ywtime/d');

        if(session('IS_ADMIN')==0){
            $user = Db::name('admin_user')->where('aid',aid)->where('id',$this->uid)->find();
            $remark = '商家设置'.t('余额宝').'提现天数，操作员：'.$user['un'];
        }else{
            $remark = '商家设置'.t('余额宝').'提现天数';
        }
        $member = Db::name('member')->where('aid',aid)->where('id',$mid)->find();
        if(!$member) return ['status'=>0,'msg'=>t('会员').'不存在'];

        Db::name('member')->where('aid',aid)->where('id',$mid)->update(['yuebao_rate'=>$ywrate,'yuebao_withdraw_time'=>$ywtime]);
        \app\common\System::plog('给会员'.$mid.'设置'.t('余额宝').'收益率、提现天数'.$ywtime);
        return json(['status'=>1,'msg'=>'操作成功']);
    }
 	//充值元宝
    public function addyuanbao(){
        if(getcustom('pay_yuanbao')){
            $mid = input('post.id/d');
            $yuanbao = intval(input('post.yuanbao'));
            $remark = input('post.remark');
            $actionname = '增加';
            if($yuanbao == 0){
                return json(['status'=>0,'msg'=>'请输入'.t('元宝').'数']);
            }
            if($yuanbao < 0) $actionname = '扣除';
            $rs = \app\common\Member::addyuanbao(aid,$mid,$yuanbao,'后台修改：'.$remark);
            \app\common\System::plog('给会员'.$mid.$actionname.t('元宝').$yuanbao);
            if($rs['status']==0) return json($rs);
            return json(['status'=>1,'msg'=>'操作成功']);
        }else{
            return json(['status'=>0,'msg'=>'操作失败']);
        }
    }
	
	//锁定
	public function dolock(){
		if(session('IS_ADMIN') == 0) return json(['status'=>1,'msg'=>'无权限操作']);
		$id = input('post.id/d');
		$st = input('post.st/d');
		Db::name('member')->where('aid',aid)->where('id',$id)->update(['islock'=>$st]);
		return json(['status'=>1,'msg'=>'操作成功']);
	}

    public function choosemember(){
        if(request()->isPost()){
            $data = Db::name('member')->where('aid',aid)->where('bid',bid)->where('id',input('post.id/d'))->find();
            if(getcustom('business_fenxiao')){
                $data['pid_nickname'] =  Db::name('member')->where('aid',aid)->where('bid',bid)->where('id',$data['pid'])->value('nickname');
            }
            return json(['status'=>1,'msg'=>'查询成功','data'=>$data]);
        }
        View::assign('from',input('param.from'));
        return View::fetch();
    }

    //加其他余额
    public function addOtherMoney(){
        if(getcustom('other_money')){
            //是否有多账户权限
            $othermoney_status = Db::name('admin')->where('id',aid)->value('othermoney_status');
            if($othermoney_status != 1){
                return json(['status'=>0,'msg'=>'无权限操作']);
            }

            $mid    = input('post.id/d');
            $type   = input('post.type');
            if($type == 'money2'){
                if($this->auth_data != 'all' && !in_array('Member/*',$this->auth_data) && !in_array('Member/addmoney2',$this->auth_data)){
                    return json(['status'=>0,'msg'=>'无权限操作']);
                }
                $type_name = t('余额2');
            }else if($type == 'money3'){
                if($this->auth_data != 'all' && !in_array('Member/*',$this->auth_data) && !in_array('Member/addmoney3',$this->auth_data)){
                    return json(['status'=>0,'msg'=>'无权限操作']);
                }
                $type_name = t('余额3');
            }else if($type == 'money4'){
                if($this->auth_data != 'all' && !in_array('Member/*',$this->auth_data) && !in_array('Member/addmoney4',$this->auth_data)){
                    return json(['status'=>0,'msg'=>'无权限操作']);
                }
                $type_name = t('余额4');
            }else if($type == 'money5'){
                if($this->auth_data != 'all' && !in_array('Member/*',$this->auth_data) && !in_array('Member/addmoney5',$this->auth_data)){
                    return json(['status'=>0,'msg'=>'无权限操作']);
                }
                $type_name = t('余额5');
            }else if($type == 'frozen_money'){
                if($this->auth_data != 'all' && !in_array('Member/*',$this->auth_data) && !in_array('Member/addfrozen_money',$this->auth_data)){
                    return json(['status'=>0,'msg'=>'无权限操作']);
                }
                $type_name = t('冻结金额');
            }else{
                return json(['status'=>0,'msg'=>'操作类型错误']);
            }
            $money  = floatval(input('post.money'));
            $remark = input('post.remark')?input('post.remark'):'';

            $actionname = '增加';
            if($money == 0 || $money == ''){
                return json(['status'=>0,'msg'=>'请输入金额']);
            }
            if($money < 0) $actionname = '扣除';
            // if(session('IS_ADMIN')==0){
            //     $user = Db::name('admin_user')->where('aid',aid)->where('id',$this->uid)->find();
            //     $remark = $remark?$remark:'商家'.$actionname.'，操作员：'.$user['un'];
            // }else{
            //     $remark = $remark?$remark:'商家'.$actionname;
            // }
            $rs = \app\common\Member::addOtherMoney(aid,$mid,$type,$money,$remark);
            \app\common\System::plog('给会员'.$mid.$actionname.$type_name.'，金额'.$money);
            if($rs['status']==0) return json($rs);
            return json(['status'=>1,'msg'=>$actionname.'成功']);
        }
    }

    public function addgongxian(){
        if(getcustom('member_gongxian')) {
            //修改贡献数
            $mid = input('post.id/d');
            $score = intval(input('post.score'));
            $remark = input('post.remark');
            $actionname = '增加';
            if($score == 0){
                return json(['status'=>0,'msg'=>'请输入'.t('贡献').'数']);
            }
            if($score < 0) $actionname = '扣除';
            $rs = \app\common\Member::addgongxian(aid,$mid,$score,'后台修改：'.$remark,'backstage');
            //更新会员消费日期
            Db::name('member')->where('aid',aid)->where('id',$mid)->update(['last_buytime'=>time()]);
            \app\common\System::plog('给会员'.$mid.$actionname.t('贡献').$score);
            if($rs['status']==0) return json($rs);
            return json(['status'=>1,'msg'=>'操作成功']);
        }
    }
    
    public function yejitongji(){
	    if(getcustom('coupon_xianxia_buy')){
            if(request()->isAjax()){
                $page = input('param.page');
                $limit = input('param.limit');
                $mid = input('param.mid');
                $couponid = input('param.couponid');
                if(input('param.field') && input('param.order')){
                    $order = input('param.field').' '.input('param.order');
                }else{
                    $order = 'id desc';
                }
                $where = [];
                $where[] = ['aid','=',aid];
                $where[] = ['mid','=',$mid];
                if(input('param.bid') == 'all'){
                }else{
                    $where[] = ['bid','=',bid];
                }
                if($couponid){
                    $where[] = ['couponid','=',$couponid];
                }
                if(input('param.id/d')) $where[] = ['coupon_record.couponid','=',input('param.id/d')];
                if(getcustom('yuyueworker_searchmember')){
                    if(input('param.mid/d')){
                        $where[] = ['coupon_record.mid','=',input('param.mid/d')];
                        $where[] = ['coupon_record.type','=',3];
                    }
                }
                if(input('param.recordmid')) $where[] = ['coupon_record.mid','=',input('param.recordmid/d')];

                if(input('param.nickname')) $where[] = ['member.nickname','like','%'.input('param.nickname').'%'];
                $count = 0 + Db::name('coupon_record')
                        ->where($where)->group('couponid')->count();
                $data = Db::name('coupon_record')
                    ->where($where)->page($page,$limit) 
                    ->group('couponid')->order($order)->select()->toArray();
               
                foreach($data as $k=>$v){
                    
                    $mycount = Db::name('coupon_record')->where('aid',aid)->where('mid',$mid)->where('bid',bid)->where('is_xianxia_buy','=',1)->where('status',0)->where('couponid',$v['couponid'])->count();
                    $mytoalcount = Db::name('coupon_record')->where('aid',aid)->where('mid',$mid)->where('bid',bid)->where('is_xianxia_buy','=',1)->where('couponid',$v['couponid'])->count();
                    $data[$k]['mycount'] = $mycount;
                    //已发放下去的 （销售数）
                    $sendcount =0+ Db::name('coupon_send')->alias('cs')
                            ->join('coupon_record cr','cs.`rid` = cr.id')
                            ->where('cs.aid',aid)
                            ->where('cs.from_mid',$mid)
                            ->where('cr.couponid',$v['couponid'])
                            ->count();
                    $myusecount  =0+ Db::name('coupon_record')
                        ->where('aid',aid)->where('mid',$v['mid'])->where('couponid',$v['couponid'])->where('is_xianxia_buy',1)->where('status',1)->where('from_mid','null')->count();
                    $data[$k]['sendcount'] = $sendcount + $myusecount;
                    $data[$k]['totalcount'] = $sendcount + $mytoalcount;
                }
                 $member = Db::name('member')->alias('m')
                     ->join('member_level ml','ml.id = m.levelid')
                     ->where('m.aid',aid)
                     ->where('m.id',$mid)
                     ->field('m.*,ml.yeji_reward_data')
                     ->find();
                $yeji_reward_data  = json_decode($member['yeji_reward_data'],true);
                 $totaldata = \app\common\Member::xianxiaYeji(aid,$member,$yeji_reward_data);
                return json(['code'=>0,'msg'=>'查询成功','count'=>$count,'data'=>$data,'totaldata' => $totaldata]);
            }
            return View::fetch();
        }
    }
    public function getTeamYeji(){
        $mid = input('param.id/d');
        $userinfo = Db::name('member')->field('id,nickname,headimg,levelid')->where('aid',aid)->where('id',$mid)->find();
        //总业绩
        $yejiwhere = [];
        $yejiwhere[] = ['status','in','1,2,3'];
        $downmids = \app\common\Member::getteammids(aid,$mid);
        if(empty($downmids)){
            return json(['status'=>0,'msg'=>'暂无团队业绩信息']);
        }
        //下级人数
        $userinfo['team_down_total'] = count($downmids);
        $userinfo['teamyeji'] = Db::name('shop_order_goods')->where('aid',aid)->where('mid','in',$downmids)->where($yejiwhere)->sum('real_totalprice');
        if ($mid && getcustom('member_level_salary_bonus')) {
            $userinfo['teamyeji_mini'] = \app\model\Commission::getMiniTeamCommission(aid,$mid);
        }
        return json(['status'=>1,'member'=>$userinfo]);
    }

    //修改分红份数
    public function addfhcopies(){
        if(getcustom('fenhong_jiaquan_bylevel')) {
            $mid = input('post.id/d');
            $copies = intval(input('post.copies'));
            $remark = input('post.remark');
            $actionname = '增加';
            if ($copies == 0) {
                return json(['status' => 0, 'msg' => '请输入份数']);
            }
            if ($copies < 0) $actionname = '扣除';
            $rs = \app\common\Member::addfhcopies(aid, $mid, $copies, '后台修改：' . $remark);
            \app\common\System::plog('给会员' . $mid . $actionname . '分红份数' . $copies);
            if ($rs['status'] == 0) return json($rs);
            return json(['status' => 1, 'msg' => '操作成功']);
        }
    }

    //回归到以前的推荐人下面
    public function huigui()
    {
        $mid = input('param.id/d');
        $member = Db::name('member')->where('aid',aid)->where('id',$mid)->find();
        if($member['pid_origin']){
            //230909 pid_origin=0改为pid_origin=null
            \app\model\Member::edit(aid,['id'=>$mid,'pid'=>$member['pid_origin'],'pid_origin'=>null,'path_origin'=>'','change_pid_time'=>time()]);
            Db::name('member_pid_changelog')->where('aid',aid)->where('mid',$mid)->where('pid_origin',$member['pid_origin'])->update(['isback'=>1,'updatetime'=>time()]);
            echo 'success';
        }
    }

    public function addtongzheng(){
        if(getcustom('product_givetongzheng')) {
            //修改贡献数
            $mid = input('post.id/d');
            $tongzheng = input('post.tongzheng');
            $remark = input('post.remark');
            $actionname = '增加';
            if($tongzheng == 0){
                return json(['status'=>0,'msg'=>'请输入'.t('通证').'数']);
            }
            if($tongzheng < 0) $actionname = '扣除';
            $rs = \app\common\Member::addtongzheng(aid,$mid,$tongzheng,'后台修改：'.$remark);
            \app\common\System::plog('给会员'.$mid.$actionname.t('贡献').$tongzheng);
            if($rs['status']==0) return json($rs);
            return json(['status'=>1,'msg'=>'操作成功']);
        }
    }
}
